﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using NLBS.Entity;
using System.Data;
using NHibernate.Criterion;

namespace NLBS.Data
{
    public partial class NHibernateDataProvider : DbProvider
    {
        #region User


       public override bool ValidateUser(string username, string password)
       {
           
           DbCommand dbCommand = db.GetSqlStringCommand("SELECT user_name, user_password FROM dbo.blog_User where user_name=@username and user_password=@password");
           db.AddInParameter(dbCommand, "username", DbType.String, username);
           db.AddInParameter(dbCommand, "password", DbType.String, password);
           IDataReader dr = db.ExecuteReader(dbCommand);
           if (dr.Read())
           {
               DbCommand updateCommand = db.GetSqlStringCommand("update blog_user set user_lastvisit=@lastvisit");
               db.AddInParameter(updateCommand, "lastvisit", DbType.DateTime, DateTime.Now);
               db.ExecuteNonQuery(updateCommand);
               return true;
           }
           return false;
       }

        public override User GetUserByID(int userID)
        {
            return CurrentSession.CreateCriteria(typeof(User))
                 .Add(Expression.Eq("UserID", userID))
                 .UniqueResult<User>();
        }

        public override User GetUserByName(string userName)
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand(@"SELECT blog_User.*,blog_UserGroup.*,blog_UserGroup.group_Name AS groupName
            FROM      blog_User,blog_UserGroup
            WHERE   (user_name = @userName and blog_User.user_groupID=blog_UserGroup.group_id)");
            db.AddInParameter(dbCommand, "userName", DbType.String, userName);

            User user = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    user = CommonDataProvider.PopulateUserViewAndEditFromIDataReader(dataReader);
                    user.Group = CommonDataProvider.PopulateUserGroupFromIDataReader(dataReader);
                }
            }
            return user;
        }



        public override IList<User> GetUser()
        {
            return CurrentSession.CreateCriteria(typeof(User)).List<User>();
        }

        public override void InsertUser(User user)
        {
            CurrentSession.Save(user);
          
        }

        public override void UpdateUser(User user)
        {
            CurrentSession.Update(user);
            CurrentSession.Flush();
        }

        public override void DeleteUser(int userID)
        {
            CurrentSession.Delete(GetUserByID(userID));
            CurrentSession.Flush();
//            DbCommand dbCommand = db.GetSqlStringCommand(@"
//            UPDATE blog_Settings SET set_value0 =set_value0-1  WHERE set_name='counterUser'");
//            db.AddInParameter(dbCommand, "user_ID", DbType.Int32, userID);
//            db.ExecuteNonQuery(dbCommand);
        }

        #endregion
    }
}
